/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.com
	E-mail: support@eleanor-cms.ru
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym
*/
// "use strict";
CORE.Question={
	Init:function(where,module){
		var th=this;
		$(".getmore",where).filter(function(){
			if($(this).data("id") && $(this).data("more") && $(this).data("pages"))
			{
				$(this).data("for",th.data.length);
				th.data.push({obj:$(this),text:[],pages:[],page:0,show:false,t:$($(this).data("more")),p:$($(this).data("pages"))});
				return true;
			}
			return false;
		}).click(function(ev,page){
			page=page||1;
			var a=$(this);
			if(typeof th.data[a.data("for")].text[page]=="undefined")
				CORE.Ajax(
					{
						language:CORE.language,
						module:module,
						event:"getmore",
						id:a.data("id"),
						page:page,
						"for":a.data("for")
					},
					function(r)
					{
						th.data[a.data("for")].text[page]=th.data[a.data("for")].t.clone(false).children().remove().end();
						th.data[a.data("for")].t.replaceWith(th.data[a.data("for")].text[page]);
						th.data[a.data("for")].t=th.data[a.data("for")].text[page].html(r["text"]);

						if(r["pages"])
						{
							th.data[a.data("for")].pages[page]=th.data[a.data("for")].p.clone(false).children().remove().end();
							th.data[a.data("for")].pages[page].insertAfter(th.data[a.data("for")].p);
							th.data[a.data("for")].p.detach();
							th.data[a.data("for")].p=th.data[a.data("for")].pages[page].html(r["pages"]);
						}
						else
							th.data[a.data("for")].p=th.data[a.data("for")].pages[page]=false;

						if(!th.data[a.data("for")].show)
						{
							th.data[a.data("for")].show=true;
							th.data[a.data("for")].t.add(th.data[a.data("for")].p).fadeIn();
						}

						th.data[a.data("for")].page=page;
						a.toggleClass("getmore getmore-active");
					}
				);
			else if(typeof arguments[1]=="undefined")
			{
				if(th.data[a.data("for")].show)
				{
					var fo=th.data[a.data("for")].t;
					if(th.data[a.data("for")].p)
						fo=fo.add(th.data[a.data("for")].p);
					fo.fadeOut();
				}
				else
				{
					var fi=th.data[a.data("for")].t;
					if(th.data[a.data("for")].p)
						fi=fi.add(th.data[a.data("for")].p);
					fi.fadeIn();
				}
				th.data[a.data("for")].show=!th.data[a.data("for")].show;
				a.toggleClass("getmore getmore-active");
			}
			else
			{
				th.data[a.data("for")].t.replaceWith(th.data[a.data("for")].text[page]);
				th.data[a.data("for")].t=th.data[a.data("for")].text[page];

				if(th.data[a.data("for")].pages[page])
				{
					th.data[a.data("for")].pages[page].insertAfter(th.data[a.data("for")].p);
					th.data[a.data("for")].p.detach();
					th.data[a.data("for")].p=th.data[a.data("for")].pages[page];
				}
				th.data[a.data("for")].page=page;
			}
			return false;
		})
	},
	data:[],//Массив с датой key : {pages:{},text:{},page:1}
	GoPage:function(p,d)
	{
		if(typeof this.data[d["for"]]!="undefined")
			this.data[d["for"]].obj.trigger("click",[p]);
	}
}
//========================================

var qDrop={
	currentDrop:undefined,
	firmDrop:undefined,
	modelDrop:undefined,
	showNum:7,
	currentPos:0,
	anyOpen:false,
	dragging:false,
	firstDrop:true,
	init:function(number){
		qDrop.showNum=number||qDrop.showNum;
		$(document).bind('click',qDrop.clickHandler);
		if (qDrop.modelDrop=$('.select_box')[0]){
		qDrop.firmDrop=$('.select_box')[0];
			qDrop.firmDrop.dropDown=$(qDrop.firmDrop).find('.drop_down');
			qDrop.firmDrop.dropList=$(qDrop.firmDrop).find('.drop_down_list');
			qDrop.firmDrop.dropList.count=$(qDrop.firmDrop).find('.drop_down_list').find('li').length-1;
			qDrop.firmDrop.scrollArea=$(qDrop.firmDrop).find('.scrollbar');
			qDrop.firmDrop.scrollElem=$(qDrop.firmDrop).find('.scrollable');
			qDrop.firmDrop.scrollElem.y=0;
			qDrop.firmDrop.input=$(qDrop.firmDrop).find('.select_box_value');
			qDrop.firmDrop.opened=false;
			qDrop.resizeScroller();
		}
	},
	resizeScroller:function(){
		if (qDrop.firmDrop){
			var percent=qDrop.showNum/qDrop.firmDrop.dropList.count;
			qDrop.firmDrop.scrollElem.height=percent*qDrop.firmDrop.scrollArea.height();
			qDrop.firmDrop.scrollElem.css('height',qDrop.firmDrop.scrollElem.height);
		}
		if (qDrop.modelDrop){
			var percent=qDrop.showNum/qDrop.modelDrop.dropList.count;
			qDrop.modelDrop.scrollElem.height=percent*qDrop.modelDrop.scrollArea.height();
			qDrop.modelDrop.scrollElem.css('height',qDrop.modelDrop.scrollElem.height);
		}
	},
	clickHandler:function(event){
		if ((event.target.className=="select_box_value" || event.target.className=="show_down_triangle") && qDrop.anyOpen){
			qDrop.showDropBox();
		}
		else if (event.target.className=="select_box_value" || event.target.className=="show_down_triangle" || event.target.className=="drop_down" || event.target.className=="drop_down_list" || event.target.className=="scrollable" || event.target.className=="scrollbar")
		{
			qDrop.showDropBox(event.target);
			qDrop.currentDrop=$('.select_box').has(event.target)[0];
			qDrop.currentDrop.scrollElem.bind('mousedown',qDrop.mouseDownHandle);
			$(document).bind('mouseup',qDrop.mouseUpHandle);
			$(document).bind('mousemove',qDrop.mouseMoveHandle);
			$(document).bind('mousewheel',qDrop.mouseScrollHandle);
			$(document).bind('selectstart',qDrop.mouseSelectHandle);
			$(document).bind('DOMMouseScroll',qDrop.mouseScrollHandle);
			$(document).bind('dragstart',function(){return false;});
		}
		else if (event.target.tagName.toUpperCase()=="LI" && event.target.parentNode && event.target.parentNode.className=="drop_down_list")
		{
			var id = $(event.target).data('id');//TODO remake to .data
			$('li',qDrop.currentDrop).each(function(){this.style.cssText=""});
			$(event.target).css({
				color:'white',
				background:'#f98c00',
				height:'16px',
				lineHeight:'16px',
				border:'1px solid #fbbc6c',
				paddingLeft:'9px'
			});
			$(qDrop.currentDrop.input).val($(event.target).html());
			$(".select_value",qDrop.currentDrop).val(id);
			if (qDrop.currentDrop==qDrop.firmDrop){
			CORE.Ajax ({
				module: module_q,
				language: CORE.language,
				event: 'select_model',
				id: id
			},
			function(data) {
				if (data) {
					$("#modeli").html(data);
				}
					qDrop.modelDrop=$('.select_box')[1];
					qDrop.modelDrop.dropDown=$(qDrop.modelDrop).find('.drop_down');
					qDrop.modelDrop.dropList=$(qDrop.modelDrop).find('.drop_down_list');
					qDrop.modelDrop.dropList.count=$(qDrop.modelDrop).find('.drop_down_list').find('li').length;
					qDrop.modelDrop.scrollArea=$(qDrop.modelDrop).find('.scrollbar');
					qDrop.modelDrop.scrollElem=$(qDrop.modelDrop).find('.scrollable');
					qDrop.modelDrop.scrollElem.y=0;
					qDrop.modelDrop.input=$(qDrop.modelDrop).find('.select_box_value');
					qDrop.modelDrop.opened=false;
					qDrop.resizeScroller();
			});
			}else{
				$(".select_value",qDrop.currentDrop).val(id);
			}
			qDrop.showDropBox();
		}
		else
		{
			if (qDrop.anyOpen)
			{
			qDrop.showDropBox();
			}
		}
		qDrop.resizeScroller();
	},
	mouseDownHandle:function(event){
		qDrop.draggin=true;
		qDrop.dragStart=event.screenY;
	},
	mouseUpHandle:function(event){
		qDrop.draggin=false;
		qDrop.currentDrop.scrollElem.y=parseInt($(qDrop.currentDrop.scrollElem).css('top'));
	},
	mouseMoveHandle:function(event){
		if (qDrop.draggin){
			var deltaY=qDrop.currentDrop.scrollElem.y+event.screenY-qDrop.dragStart;
//	console.log(qDrop.firmDrop.dropList);
			if (deltaY<0) deltaY=0;
			if (deltaY+qDrop.currentDrop.scrollElem.height>qDrop.currentDrop.scrollArea.height()) deltaY=qDrop.currentDrop.scrollArea.height()-qDrop.currentDrop.scrollElem.height;

			var koef=qDrop.currentDrop.scrollArea.height()/(qDrop.currentDrop.dropList.count*18);
			$(qDrop.currentDrop.scrollElem).css({top:deltaY});
			qDrop.currentDrop.dropList.css('top',-deltaY/koef);
		}
	},
	mouseScrollHandle:function(event,delta){
		console.log(qDrop.currentDrop)
		var koef=qDrop.currentDrop.scrollArea.height()/(qDrop.currentDrop.dropList.count*18);

		delta=event.originalEvent.wheelDelta?event.originalEvent.wheelDelta/120:-event.originalEvent.detail/3;

		var deltaY=qDrop.currentDrop.scrollElem.y-(delta)*5;
		if (deltaY<0) deltaY=0;
		if (deltaY+qDrop.currentDrop.scrollElem.height>qDrop.currentDrop.scrollArea.height()) deltaY=qDrop.currentDrop.scrollArea.height()-qDrop.currentDrop.scrollElem.height;



			$(qDrop.currentDrop.scrollElem).css({top:deltaY});
			qDrop.currentDrop.dropList.css('top',-deltaY/koef);
		qDrop.currentDrop.scrollElem.y=deltaY;
		if (event.preventDefault)
			event.preventDefault();
		event.returnValue=false;
		event.cancelBubble=true;
		return false;
	},
	mouseSelectHandle:function(event){
		if (event.preventDefault)
		event.preventDefault();
		return false;
	},
	showDropBox:function(evTarget){
		if (evTarget)
		{
			$(evTarget.parentNode.dropDown).css({height:qDrop.showNum*18,display:'block'})
			qDrop.anyOpen=true;
		}
		else
		{
			if (qDrop.anyOpen)
			{
				if (qDrop.firmDrop){
					$(qDrop.firmDrop.dropDown).css({display:'none'})
				}
				if (qDrop.modelDrop){
					$(qDrop.modelDrop.dropDown).css({display:'none'})
				}
				qDrop.currentDrop.scrollElem.unbind('mousedown',qDrop.mouseDownHandle);
				$(document).unbind('mouseup',qDrop.mouseUpHandle);
				$(document).unbind('mousemove',qDrop.mouseMoveHandle);
				$(document).unbind('mousewheel',qDrop.mouseScrollHandle);
				$(document).unbind('DOMMouseScroll',qDrop.mouseScrollHandle);
				$(document).unbind('selectstart',qDrop.mouseSelectHandle);
				$(document).unbind('dragstart');
				qDrop.anyOpen=false;
				qDrop.currentDrop=undefined;
			}
		}
	}
}
$(function(){qDrop.init()});


////////////////////////////////////////////////////////////////////////////////////
var rules_check = false;
var valid = false;
function valideForm() {
	if (rules_check)
		valid = true;
	else
		valid = false;
	if (!valid || !$('#q_question').val() || !$('#q_text').val()) {
		$('#form_send_status').show();
		$('#form_send_status').removeClass('green').addClass('red');
		$('#form_send_status_text').text(CORE.Lang("form_error"));
	}
	return false;
}

$(function () {
	$("#checkbox").off('click.addCheck').on('click.addCheck', function() {
		if (this.className == "checkbox_checked") {
			this.className = "checkbox_unchecked";
			rules_check = false;
			return false;
		} else {
			this.className = "checkbox_checked";
			rules_check = true;
			return false;
		}
	});
	$('#add_your_question_form').on('submit', function () {
		valideForm();
		var question = $('#q_question').val();
		var text = $('#q_text').val();
		var marki_id = $('#marka').val();
		var modeli_id = $('#modeli_select').val();
		var year = $('#year').val();
		if (question && text && rules_check) {
			CORE.Ajax ({
					module: module_q,
					language: CORE.language,
					event: 'save',
					question: question,
					text: text,
					marki: marki_id,
					modeli: modeli_id,
					year: year,
					author_name: user_n
				},
				function (res) {
					// console.log(res.error);
					$('#form_send_status').show().removeClass('red').addClass('show green').fadeOut(4000);
					$('#form_send_status_text').text(CORE.Lang("form_sent"));
				}
			)
		}
	});
});